<?php

include "../site_specific/project.php";
include "../includes/constants.php";
include "../site_specific/external_constants.php";
include "../includes/phpmailer/class.phpmailer.php";
include "../includes/phpmailer/class.smtp.php";
include "../includes/utilities.php";
include "../includes/pages.php";
include "../includes/database.php";
include "../includes/users.php";
include "../includes/date.php";
include "../includes/text.php";
include "../includes/files.php";

# Establish database connection
establishDatabaseConnection();

$parent_id=$_GET["id"];

$sql_pdf="SELECT * FROM apollo_pages WHERE p_id=\"".$parent_id."\" AND p_status<3 ORDER BY p_sort ASC, p_name ASC";

$data_current_page=getRecord($sql_pdf);

if(in_array($data_current_page[0]["p_parent"],array("mres_7014","mres_7005","mres_7007","mres_7009","mres_7016","mres_7008","mres7100","mres_7001","mres_7002","mres_7003","mres_7004","mres_7006","mres_7013"))){
	$sql_pdf_module="SELECT * FROM apollo_pages WHERE p_parent=\"".$parent_id."\" AND p_status<3 ORDER BY p_sort ASC, p_name ASC";
	$data_pdf=getRecord($sql_pdf_module);	
}
else{
	$data_pdf=$data_current_page;
}

$content="";
require('../site_specific/html2fpdf.php');

$pdf=new HTML2FPDF('P','mm','A4');


foreach($data_pdf as $data_pdf_key => $data_pdf_value){
	$content = parseFlashLinks(parseFlashXML($data_pdf_value["p_content"]));
	
	$content = remove_flash($content);

	$table_removed=preg_replace("#\<table(.+?)\>#", "", $content);

	$table_removed=str_replace("</table>", "", $table_removed);

	$table_removed=preg_replace("#\<tr(.+?)\>#", "", $table_removed);

	$table_removed=preg_replace("#\<td(.+?)\>#", "", $table_removed);

	$table_removed=str_replace("</tr>", "", $table_removed);
	
	$table_removed=str_replace("</td>", "", $table_removed);
	
	$table_removed=str_replace("<a href=\"www.uq.edu.au\">solution</a>","",$table_removed);
	
	#Replace the url for hyperlink for PDF generation
	$table_removed = replace_hyperlink_url($table_removed);
	
	$table_removed=RemoveHyperlink($table_removed,"***","###");
	
	$table_removed=RemoveHyperlink($table_removed,"*<*","*>*");
	
	$table_removed=RemoveHyperlink($table_removed,"*&lt;*","*&gt;*");
	
	$multiple_choice_searching_item_begin="<multiple-choice>";
						  
	$multiple_choice_searching_item_end="</multiple-choice>";
						  
	if(strpos($table_removed,$multiple_choice_searching_item_begin)&& strpos($table_removed,$multiple_choice_searching_item_begin)){
		
		$table_removed=ReplaceMulitpleChoice($table_removed,true,false);
	}
	
	$html_symbol=array('&#915;','&#916;','&#920;','&#923;','&#926;','&#928;','&#931;','&#934;','&#936;','&#937;','&#945;','&#946;','&#947;','&#948;','&#949;','&#950;','&#951;','&#952;','&#953;','&#954;','&#955;','&#956;','&#957;','&#958;','&#960;','&#962;','&#963;','&#964;','&#965;','&#966;','&#967;','&#968;','&#969;','&#977;','&#978;','&#982;','&#961;','&#8776;','&#8869;','&#8593;','&#8595;','&#8704;','&#8730;','&#8805;','&#8226;','&#9002;','&#8747;','&#914;');
	$style="font-family: symbol;";

	$css_symbol=array("<span style=\"".$style."\">F</span>",
				  	  "<span style=\"".$style."\">D</span>",
				      "<span style=\"".$style."\">Q</span>",
					  "<span style=\"".$style."\">L</span>",
					  "<span style=\"".$style."\">X</span>",
					  "<span style=\"".$style."\">P</span>",
					  "<span style=\"".$style."\">S</span>",
					  "<span style=\"".$style."\">F</span>",
					  "<span style=\"".$style."\">Y</span>",
					  "<span style=\"".$style."\">W</span>",
					  "<span style=\"".$style."\">a</span>",
					  "<span style=\"".$style."\">b</span>",
					  "<span style=\"".$style."\">g</span>",
					  "<span style=\"".$style."\">d</span>",
					  "<span style=\"".$style."\">e</span>",
					  "<span style=\"".$style."\">z</span>",
					  "<span style=\"".$style."\">h</span>",
					  "<span style=\"".$style."\">q</span>",
					  "<span style=\"".$style."\">i</span>",
					  "<span style=\"".$style."\">k</span>",
					  "<span style=\"".$style."\">l</span>",
					  "<span style=\"".$style."\">m</span>",
					  "<span style=\"".$style."\">n</span>",
					  "<span style=\"".$style."\">x</span>",
					  "<span style=\"".$style."\">p</span>",
					  "<span style=\"".$style."\">V</span>",
					  "<span style=\"".$style."\">s</span>",
					  "<span style=\"".$style."\">t</span>",
					  "<span style=\"".$style."\">u</span>",
					  "<span style=\"".$style."\">j</span>",
					  "<span style=\"".$style."\">c</span>",
					  "<span style=\"".$style."\">y</span>",
					  "<span style=\"".$style."\">w</span>",
					  "<span style=\"".$style."\">j</span>",
					  "<span style=\"".$style."\">g</span>",
					  "<span style=\"".$style."\">v</span>",
					  "<span style=\"".$style."\">r</span>",
					  "<span style=\"".$style."\">&#187;</span>",
					  "<span style=\"".$style."\">&#94;</span>",
					  "<span style=\"".$style."\">&#173;</span>",
					  "<span style=\"".$style."\">&#175;</span>",
					  "<span style=\"".$style."\">&#34;</span>",
					  "<span style=\"".$style."\">&#214;</span>",
					  "<span style=\"".$style."\">&#179;</span>",
					  "<span style=\"".$style."\">&#183;</span>",
					  "<span style=\"".$style."\">&#241;</span>",
					  "<span style=\"".$style."\">&#242;</span>","&#66;");
	$symbol_replaced=str_replace($html_symbol, $css_symbol, $table_removed);

	$sql_pdf_select_module="SELECT * FROM apollo_pages WHERE p_id=\"".$data_pdf_value["p_parent"]."\" AND p_status<3";
	
	$data_select_module=getRecord($sql_pdf_select_module);
	
	$sql_pdf_select_course="SELECT * FROM apollo_pages WHERE p_id=\"".$data_select_module[0]["p_parent"]."\" AND p_status<3";
	
	$data_select_course=getRecord($sql_pdf_select_course);
	
	$header_course_name=explode("-",$data_select_course[0]["p_name"]);
	
	$symbol_replaced=$symbol_replaced;
	
	$pdf->SetTitle($data_select_module[0]["p_name"]);
	
	$pdf->AddPage();//"<p align=\"right\"><I>".$header_course_name[0].":</I><B><I>".$data_select_module[0]["p_name"]."</I></B></p>"
	$pdf->WriteHTML($symbol_replaced);
}

$pdf->Output($parent_id,'');

exit();


?>